cmake_minimum_required(VERSION 3.16)

project(i2pdtunnelwizard LANGUAGES CXX)

set(HTML_TEMPLATE_FILE "${CMAKE_SOURCE_DIR}/htmldata.cpp")

if(NOT EXISTS "${HTML_TEMPLATE_FILE}")
    file(WRITE "${HTML_TEMPLATE_FILE}" "// This file was created automatically\n")
endif()

function(add_html_content html_file const_name)
    file(READ "${html_file}" HTML_CONTENT)
    string(REPLACE "\n" " " HTML_CONTENT "${HTML_CONTENT}")
    string(REGEX REPLACE " + " " " HTML_CONTENT "${HTML_CONTENT}")
    string(REGEX REPLACE "^ +| +$" "" HTML_CONTENT "${HTML_CONTENT}")

    # Проверка на существование константы
    file(READ "${HTML_TEMPLATE_FILE}" TEMPLATE_CONTENT)
    if("${TEMPLATE_CONTENT}" MATCHES "${const_name}")
        message(STATUS "${const_name} already defined")
    else()
        file(APPEND "${HTML_TEMPLATE_FILE}" "const char* ${const_name} = R\"(")
        file(APPEND "${HTML_TEMPLATE_FILE}" "${HTML_CONTENT}")
        file(APPEND "${HTML_TEMPLATE_FILE}" ")\";\n")
    endif()
endfunction()

add_html_content("${CMAKE_SOURCE_DIR}/html/main_page.html" "HTML_MAIN_PAGE")
add_html_content("${CMAKE_SOURCE_DIR}/html/config_page.html" "HTML_CONFIG_PAGE")
add_html_content("${CMAKE_SOURCE_DIR}/html/error_page.html" "HTML_ERROR_PAGE")

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(i2pdtunnelwizard main.cpp
    "${HTML_TEMPLATE_FILE}"
    htmldata.h
    versionnumber.h
    tunnelconstructor.h tunnelconstructor.cpp
    notepad.h notepad.cpp
    tunneltype.h
    randomstringgenerator.h randomstringgenerator.cpp
    randomstringgenerator.h randomstringgenerator.cpp
    memorymanagment.h memorymanagment.cpp
    http/crow.h
    http/crow/app.h
    http/crow/ci_map.h
    http/crow/common.h
    http/crow/compression.h
    http/crow/exceptions.h
    http/crow/http_connection.h
    http/crow/http_parser_merged.h
    http/crow/http_request.h
    http/crow/http_response.h
    http/crow/http_server.h
    http/crow/json.h
    http/crow/logging.h
    http/crow/middleware.h
    http/crow/middleware_context.h
    http/crow/mime_types.h
    http/crow/multipart.h
    http/crow/multipart_view.h
    http/crow/mustache.h
    http/crow/parser.h
    http/crow/query_string.h
    http/crow/returnable.h
    http/crow/routing.h
    http/crow/settings.h
    http/crow/socket_adaptors.h
    http/crow/task_timer.h
    http/crow/utility.h
    http/crow/version.h
    http/crow/websocket.h
    http/crow/middlewares/cookie_parser.h
    http/crow/middlewares/cors.h
    http/crow/middlewares/session.h
    http/crow/middlewares/utf-8.h
    http/httpserver.h http/httpserver.cpp
    http/languagehandlermiddleware.h http/languagehandlermiddleware.cpp)

include(GNUInstallDirs)
install(TARGETS i2pdtunnelwizard
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
